# coding=utf-8
# Copyright 2021 The Uncertainty Baselines Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Uncertainty baseline training models."""

import warnings
import tensorflow as tf

from uncertainty_baselines.models import efficientnet_utils
from uncertainty_baselines.models.criteo_mlp import create_model as CriteoMlpBuilder
from uncertainty_baselines.models.efficientnet import create_model as EfficientNetBuilder
from uncertainty_baselines.models.efficientnet_batch_ensemble import create_model as EfficientNetBatchEnsembleBuilder
from uncertainty_baselines.models.genomics_cnn import create_model as GenomicsCNNBuilder
from uncertainty_baselines.models.models import get
from uncertainty_baselines.models.movielens import create_model as MovieLensBuilder
from uncertainty_baselines.models.resnet20 import create_model as ResNet20Builder
from uncertainty_baselines.models.resnet50 import create_model as ResNet50Builder
from uncertainty_baselines.models.resnet50_batchensemble import resnet101_batchensemble
from uncertainty_baselines.models.resnet50_batchensemble import resnet50_batchensemble
from uncertainty_baselines.models.resnet50_batchensemble import resnet_batchensemble
from uncertainty_baselines.models.resnet50_deterministic import resnet50_deterministic
from uncertainty_baselines.models.resnet50_dropout import resnet50_dropout
from uncertainty_baselines.models.resnet50_het_mimo import resnet50_het_mimo
from uncertainty_baselines.models.resnet50_het_rank1 import resnet50_het_rank1
from uncertainty_baselines.models.resnet50_heteroscedastic import resnet50_heteroscedastic
from uncertainty_baselines.models.resnet50_radial import resnet50_radial
from uncertainty_baselines.models.resnet50_rank1 import resnet50_rank1
from uncertainty_baselines.models.resnet50_sngp import resnet50_sngp
from uncertainty_baselines.models.resnet50_sngp import resnet50_sngp_add_last_layer
from uncertainty_baselines.models.resnet50_sngp_be import resnet50_sngp_be
from uncertainty_baselines.models.resnet50_variational import resnet50_variational
from uncertainty_baselines.models.textcnn import create_model as TextCNNBuilder
from uncertainty_baselines.models.wide_resnet import create_model as WideResNetBuilder
from uncertainty_baselines.models.wide_resnet import wide_resnet
from uncertainty_baselines.models.wide_resnet_batchensemble import wide_resnet_batchensemble
from uncertainty_baselines.models.wide_resnet_condconv import wide_resnet_condconv
from uncertainty_baselines.models.wide_resnet_dropout import wide_resnet_dropout
from uncertainty_baselines.models.wide_resnet_heteroscedastic import wide_resnet_heteroscedastic
from uncertainty_baselines.models.wide_resnet_hyperbatchensemble import e_factory as hyperbatchensemble_e_factory
from uncertainty_baselines.models.wide_resnet_hyperbatchensemble import LambdaConfig as HyperBatchEnsembleLambdaConfig
from uncertainty_baselines.models.wide_resnet_hyperbatchensemble import wide_resnet_hyperbatchensemble
from uncertainty_baselines.models.wide_resnet_rank1 import wide_resnet_rank1
from uncertainty_baselines.models.wide_resnet_sngp import wide_resnet_sngp
from uncertainty_baselines.models.wide_resnet_sngp_be import wide_resnet_sngp_be
from uncertainty_baselines.models.wide_resnet_variational import wide_resnet_variational

# When adding a new model, also add to models.py for easier user access.

# pylint: disable=g-import-not-at-top
try:
  # Try to import models depending on dune.
  from uncertainty_baselines.models import vit_batchensemble
  from uncertainty_baselines.models.vit_batchensemble import PatchTransformerBE
except ImportError as e:
  warnings.warn(f'Skipped ViT models due to ImportError: {e}')
except tf.errors.NotFoundError as e:
  warnings.warn(f'Skipped ViT models due to NotFoundError: {e}')
try:
  # Try to import models depending on tensorflow_models.official.nlp.
  from uncertainty_baselines.models.bert import create_model as BertBuilder
  from uncertainty_baselines.models.bert_dropout import create_model as DropoutBertBuilder
  from uncertainty_baselines.models.bert_sngp import create_model as SngpBertBuilder
except ImportError as e:
  warnings.warn(f'Skipped BERT models due to ImportError: {e}')
except tf.errors.NotFoundError as e:
  warnings.warn(f'Skipped BERT models due to NotFoundError: {e}')
try:
  # Try to import models depending on edward2.experimental.mimo.
  from uncertainty_baselines.models.resnet50_mimo import resnet50_mimo
  from uncertainty_baselines.models.wide_resnet_mimo import wide_resnet_mimo
except ImportError as e:
  warnings.warn(f'Skipped MIMO models due to ImportError: {e}')
except tf.errors.NotFoundError as e:
  warnings.warn(f'Skipped MIMO models due to NotFoundError: {e}')


try:
  from uncertainty_baselines.models import vit_batchensemble
  from uncertainty_baselines.models.vit_batchensemble import PatchTransformerBE
except ImportError as e:
  warnings.warn(f'Skipped importing Flax ViT models due to ImportError: {e}')

# This is necessary because we cannot depend on torch internally, so the torch
# model modules cannot be imported at all, so we cannot just wrap the imports in
# a try/except.
import_torch = True
if import_torch:
  from uncertainty_baselines.models.resnet50_torch import resnet50_dropout_torch
# pylint: enable=g-import-not-at-top
